/*
 * @Author: szx
 * @Date: 2021-12-24 15:32:28
 * @LastEditTime: 2021-12-24 15:59:48
 * @Description:
 * @FilePath: \leetcode\500-599\501\501.js
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */

var findMode = function (root) {
    let pre = null;
    let res = 1;
    let ans = [];
    let max = 1;
    const dfs = (root) => {
        if (!root) return;
        dfs(root.left);
        if (pre) {
            if (root.val == pre) max++;
            else {
                if (max > res) {
                    ans = [pre];
                    res = max;
                } else if (max == res) {
                    ans.push(pre);
                }
                max = 1;
            }
        }
        pre = root.val;
        dfs(root.right);
    };
    dfs(root);
    if (max > res) {
        ans = [];
        ans.push(pre);
        res = max;
    } else if (max == res) {
        ans.push(pre);
    }
    return ans;
};
